package com.bw.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.bw.domain.Result;
import com.bw.domain.SysGoods;
import com.bw.service.SysGoodsService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author bobo棒
 * @date 2024年04月15日 11:01
 */
@CrossOrigin
@RequestMapping("goods")
@RestController
public class GoodsController {
    @Autowired
    SysGoodsService sysGoodsService;
    @Autowired
    RedisTemplate redisTemplate;


    @RequestMapping("test")
    public String test(){
        return "6666";
    }
    
    @RequestMapping("addOrUpdate")
    public Result addOrUpdate(@RequestBody SysGoods sysGoods){
        String key = DigestUtil.md5Hex(sysGoods.toString());
        Boolean aBoolean = redisTemplate.opsForValue().setIfAbsent(key, 1, 1, TimeUnit.MINUTES);
        if(aBoolean){
            boolean b = sysGoodsService.saveOrUpdate(sysGoods);
            return b?Result.success():Result.fail();
        }
        return Result.fail("重复提交");
    }

    @RequestMapping("list")
    public Result list(String name, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
      List<SysGoods> list= sysGoodsService.listGoods(name);
        PageInfo<SysGoods> info = new PageInfo<>(list);
        return Result.success(info);

    }
}
